/*
 * author      : 吴中勤 
 * date        : 2018/8/22
 * description : TODO
 */

package com.deepexi.adapter.rest.sap;

import com.deepexi.adapter.common.sdk.RequestInfo;
import com.deepexi.adapter.facade.sap.SapPushService;
import com.deepexi.adapter.rest.sap.parser.*;
import com.deepexi.adapter.sdk.moby.MobySdk;
import com.deepexi.adapter.sdk.moby.response.BaseMobyResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


@Service
public class SapPushServiceImpl implements SapPushService {
    private static final Logger logger = LoggerFactory.getLogger(SapPushServiceImpl.class);

    private static final int THREAD_POOL_SIZE = 5;
    private ExecutorService executor = Executors.newFixedThreadPool(THREAD_POOL_SIZE);

    @Autowired
    private MobySdk mobySdk;

    @Autowired
    private CommonSapPushHandler commonSapPushHandler;

    @Override
    public void onReceiveMaterial(String json) {
        executor.submit(() -> {
            _onReceiveMaterial(json);
        });
    }

    /**
     * 同步方法, 处理物料同步
     *
     * @return 是否成功
     */
    public boolean _onReceiveMaterial(String json) {
        // 处理 入参 json
        SapPushMaterialParser parser = new SapPushMaterialParser();
        parser.parse(json);
        RequestInfo requestInfo = mobySdk.requestInfo4Material(parser.getMobyRequestBodyList());
        return _sendMobyAndCbSap(requestInfo, parser.getKeyNumList());
    }

    @Override
    public void onReceiveVehicleModelLine(String json) {
        executor.submit(() -> {
            _onReceiveVehicleModelLine(json);
        });
    }

    public boolean _onReceiveVehicleModelLine(String json) {
        SapPushModelLineParser parser = new SapPushModelLineParser();
        parser.parse(json);
        RequestInfo requestInfo = mobySdk.requestInfo4VehicleModelLine(parser.getMobyRequestBodyList());
        return _sendMobyAndCbSap(requestInfo, parser.getKeyNumList());
    }

    @Override
    public void onReceiveVehicleModel(String json) {
        executor.submit(() -> {
            _onReceiveVehicleModel(json);
        });
    }

    public boolean _onReceiveVehicleModel(String json) {
        SapPushModelParser parser = new SapPushModelParser();
        parser.parse(json);
        RequestInfo requestInfo = mobySdk.requestInfo4VehicleModel(parser.getMobyRequestBodyList());
        return _sendMobyAndCbSap(requestInfo, parser.getKeyNumList());
    }

    @Override
    public void onReceiveInteriorPart(String json) {
        executor.submit(() -> {
            _onReceiveInteriorPart(json);
        });
    }

    public boolean _onReceiveInteriorPart(String json) {
        SapPushInteriorPartParser parser = new SapPushInteriorPartParser();
        parser.parse(json);
        RequestInfo requestInfo = mobySdk.requestInfo4InteriorPart(parser.getMobyRequestBodyList());
        return _sendMobyAndCbSap(requestInfo, parser.getKeyNumList());
    }

    @Override
    public void onReceiveExteriorPart(String json) {
        executor.submit(() -> {
            _onReceiveExteriorPart(json);
        });
    }

    public boolean _onReceiveExteriorPart(String json) {
        SapPushExteriorPartParser parser = new SapPushExteriorPartParser();
        parser.parse(json);
        RequestInfo requestInfo = mobySdk.requestInfo4ExteriorPart(parser.getMobyRequestBodyList());
        return _sendMobyAndCbSap(requestInfo, parser.getKeyNumList());
    }

    @Override
    public void onReceiveVehicleInfo(String json) {
        executor.submit(() -> {
            _onReceiveVehicleInfo(json);
        });
    }

    public boolean _onReceiveVehicleInfo(String json) {
        SapPushVehicleInfoParser parser = new SapPushVehicleInfoParser();
        parser.parse(json);
        RequestInfo requestInfo = mobySdk.requestInfo4VehicleInfo(parser.getMobyRequestBodyList());
        return _sendMobyAndCbSap(requestInfo, parser.getKeyNumList());
    }


    /**
     * 1 发送请求到 moby
     * 2 回调sap
     */
    private boolean _sendMobyAndCbSap(RequestInfo requestInfo, List<String> keyNumList) {
        try {
            String result = commonSapPushHandler.requestMobyAndSaveLog(requestInfo);
            BaseMobyResponse mobyResponse = mobySdk.parseResult(requestInfo, result);
            if (mobyResponse.isSuccess()) {
                commonSapPushHandler.callbackToSap(keyNumList);
            } else {
                logger.error("_sendMobyAndCbSap, moby response error");
            }
            return mobyResponse.isSuccess();
        } catch (Exception ex) {
            logger.error("_sendMobyAndCbSap, ex:", ex);
            return false;
        }
    }

}
